const app = getApp()
import BaseModel from '../../utils/common.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    depart_name:"",//添加部门
    isShow:false,//弹窗是否显示
    page:1,
    size:12, //每页条数
    departmentAllData:[], //部门数据
    staffAllData:[],//全部员工数据
    countAll:"", //全部员工数量
    footer_more:"",
    empty_title:"",
    empty_img:"",
    empty_again:"",//重试
    is_clickTab:0 //重试或下拉是0，上啦是1
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  onShow: function () {
    this.getDepartmentAllData(); //部门数据
    this.getStaffAllData(); //全部员工数据
  },
  /*部门数据*/
  getDepartmentAllDataModel: new BaseModel({
    url: 'Department',
    method: "get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getDepartmentAllData() {
    this.getDepartmentAllDataModel
        .fetch({ })
        .then(res => {
          this.setData({
            departmentAllData: res.data
          })
        })
        .catch(res => {
          //失败
        })
  },
  /*全部员工数据*/
  getStaffAllDataModel: new BaseModel({ 
    url: 'getStaff',
    method: "get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getStaffAllData() {
    this.getStaffAllDataModel
        .fetch({
          "page": this.data.page,
          "size": this.data.size
        })
        .then(res => {
          if(this.data.is_clickTab==0){
            this.setData({
              staffAllData: res.data,
              countAll:res.count,
              empty_title:"暂无数据",
              empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon_new3.0/empty_mes.png",
              empty_again:"",//重试
            })
          }else{
            if(res.data==""||res.data==null){
              this.setData({
                footer_more:"没有更多数据了"
              })
            }else{
              //成功
              var staffAllData_list = this.data.staffAllData;
              for(var i=0;i<res.data.length;i++){
                staffAllData_list.push(res.data[i])
              }
              this.setData({
                staffAllData: staffAllData_list,
                countAll:res.count
              })
            }
          }
        })
        .catch(res => {
          //失败
          this.setData({
            empty_title:"请求失败",
            empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/empty_data.png",
            empty_again:"重试",//重试
          })
        })
  },

  //提交部门
  formSubmit(e){
    this.setData({
      depart_name:e.detail.value.depart_name
    })
    if(e.detail.value.depart_name==""){
      wx.showToast({
        title: '部门是空的哦',
        icon: 'none',
      })
    }else{
      this.getDepartmentData(); //提交部门
      this.getDepartmentAllData(); //获取部门列表
    }
  },
  /*提交部门*/
  getDepartmentModel: new BaseModel({
    url: 'addDepartment',
      needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getDepartmentData() {
    this.getDepartmentModel
        .fetch({
          depart_name:this.data.depart_name
        })
        .then(res => {
          this.setData({
            isShow:false
          })
        })
        .catch(res => {
          //失败
        })
  },
  department_add:function(){
    this.setData({
      isShow:true
    })
  },
  //点击弹窗灰色部分隐藏
  clickMaskBg:function(){
    this.setData({
      isShow:false
    })
  },
  //重试
  again_request:function(){
    this.setData({
      is_clickTab:0,
      page:1
    })
    this.getStaffAllData();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      is_clickTab:0,
      page:1
    })
    this.getDepartmentAllData(); //部门数据
    this.getStaffAllData(); //全部员工数据
    wx.stopPullDownRefresh(); //停止下拉
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    that.data.page+=1;
    this.setData({
      is_clickTab:1
    })
    this.getStaffAllData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
})